using System.Collections;
using System.Collections.Generic;
using Base;
using UnityEngine;
using UnityEngine.UI;

public class MallItem : MonoBehaviour
{
   
    [SerializeField]  Text nameLabel;
    [SerializeField]  Image mallIcon;
    [SerializeField]  Text levelLabel;
    [SerializeField]  Image selectBG;
    [SerializeField]  Text goldLabel;
    [SerializeField]  Button selectBtn;
    public UpgradeItem model;
    void Start()
    {
        selectBtn.onClick.AddListener(OnSelect);
        nameLabel.text =model.name;
        mallIcon.sprite = Resources.Load<Sprite>(model.icon);
        showUI();
    }

    private void showUI()
    {
         switch (model.id)
        {
            case 0:
                int armorLevel = GameManager.Instance.playerData.upgradeArmorLevel;
                if (armorLevel == model.levels.Length)
                {
                    levelLabel.text = "最高等级";
                    selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_gray");
                    selectBtn.interactable = false;
                }
                else
                {
                    levelLabel.text = "等级"+(armorLevel+1);
                    goldLabel.text = model.levels[armorLevel].cost.ToString();
                    if (GameManager.Instance.playerData.playerGold >= model.levels[armorLevel].cost)
                    {
                        selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_green");
                        selectBtn.interactable = true;
                    }
                    else
                    {
                        selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_gray");
                        selectBtn.interactable = false;
                    }
                }
                break;
            case 1:
                int damageLevel = GameManager.Instance.playerData.upgradeDamageLevel;
                if (damageLevel == model.levels.Length)
                {
                    levelLabel.text = "最高等级";
                    selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_gray");
                    selectBtn.interactable = false;
                }
                else
                {
                    levelLabel.text = "等级"+(damageLevel+1);
                    goldLabel.text = model.levels[damageLevel].cost.ToString();
                    if (GameManager.Instance.playerData.playerGold >= model.levels[damageLevel].cost)
                    {
                        selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_green");
                        selectBtn.interactable = true;
                    }
                    else
                    {
                        selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_gray");
                        selectBtn.interactable = false;
                    }
                }
                break;
            case 2:
                int healthLevel = GameManager.Instance.playerData.upgradeHealthLevel;
                if (healthLevel == model.levels.Length)
                {
                    levelLabel.text = "最高等级";
                    selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_gray");
                    selectBtn.interactable = false;
                }
                else
                {
                    levelLabel.text = "等级"+(healthLevel+1);
                    goldLabel.text = model.levels[healthLevel].cost.ToString();
                    if (GameManager.Instance.playerData.playerGold >= model.levels[healthLevel].cost)
                    {
                        selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_green");
                        selectBtn.interactable = true;
                    }
                    else
                    {
                        selectBG.sprite = Resources.Load<Sprite>("Sprites/UI/Generic/ui_button_gray");
                        selectBtn.interactable = false;
                    }
                }
                break;
            
        }
    }


    private void OnSelect()
    {
        
        switch (model.id)
        {
            case 0:
                GameManager.Instance.playerData.playerGold -= model.levels[GameManager.Instance.playerData.upgradeArmorLevel].cost;
                GameManager.Instance.playerData.upgradeArmorLevel ++;
                GameManager.Instance.OnPlayerDataSave();
                break;
            case 1:
                GameManager.Instance.playerData.playerGold -= model.levels[GameManager.Instance.playerData.upgradeDamageLevel].cost;

                GameManager.Instance.playerData.upgradeDamageLevel ++;
                GameManager.Instance.OnPlayerDataSave();
                break;
            case 2:
                GameManager.Instance.playerData.playerGold -= model.levels[GameManager.Instance.playerData.upgradeHealthLevel].cost;
                GameManager.Instance.playerData.upgradeHealthLevel ++;
                GameManager.Instance.OnPlayerDataSave();
                break;
        }
        EventCenterManager.Instance.Dispatch(EventCommand.UpgradeSelect);
        showUI();
    }
}
